package org.leetcode.easy.leetcode121;

import java.util.Map;

public class Solution {
    public int maxProfit5(int[] prices){

        int maxProfit =0;
        int minPrice = prices[0];

        for (int i = 0; i < prices.length; i++) {
            if (prices[i]<minPrice){
                minPrice=prices[i];
            }else {
                maxProfit=Math.max(maxProfit,prices[i]-minPrice);
            }
        }


        return maxProfit;
    }

    public int maxProfit3(int[] prices){

        //最低的入手价格
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;

        for (int i = 0; i < prices.length; i++) {

            if (prices[i]<minPrice){
                minPrice=prices[i];
            }else if (prices[i]-minPrice>maxProfit){
                maxProfit=prices[i]-minPrice;
            }

        }

        return maxProfit;
    }
    public int maxProfit2(int[] prices) {
        int minPrice = Integer.MAX_VALUE;
        int maxProfit=0;

        for (int i = 0; i < prices.length; i++) {
            if (prices[i]<minPrice) {
                minPrice=prices[i];
            }else if (prices[i]-minPrice>maxProfit){
                maxProfit=prices[i]-minPrice;
            }
        }

        return maxProfit;
    }


    //超时
    public int maxProfit(int[] prices) {
        int number = 0;
        int slow =0;
        int fast =slow+1;

        while (fast>slow)
        {
            if (slow==prices.length-1)
                break;

            number= Math.max(number,prices[fast]-prices[slow]);

            if (fast==prices.length-1)
            {
                slow++;
                fast=slow;
            }
            fast++;
        }

        return number;
    }

    public static void main(String[] args) {
        int []prices ={7,1,5,3,6,4};

        Solution solution = new Solution();

        int maxProfit = solution.maxProfit(prices);
        System.out.println(maxProfit);
    }
}
